package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.MenuMapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.pojo.Menu;
import com.example.demo.pojo.RoleMenu;
import com.example.demo.utils.LayuiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class EndRoleMenuController {


    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;


    /**
     * 添加用户角色权限
     * @param roleMenu
     * @return
     */
    @RequestMapping("/add")
    public Object get(@RequestBody RoleMenu roleMenu){
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleMenu.getRoleId()).eq("menu_id",roleMenu.getMenuId());
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(queryWrapper);
        if(roleMenus.size() > 0){
            return LayuiResult.LayuiData(0,"当前用户已存在该权限，请勿重复添加!",null);
        }
        // 当前要添加的菜单权限
        Menu menu = menuMapper.selectById(roleMenu.getMenuId());
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menu.getParentId()).eq("role_id",roleMenu.getRoleId());
        List<RoleMenu> roleMenusParent = roleMenuMapper.selectList(queryWrapper);
        if(roleMenusParent.size() > 0){
            // 如果父菜单存在，直接添加子菜单权限
            roleMenuMapper.insert(roleMenu);
        }else {
            // 如果父菜单不存在，先添加父菜单权限
            RoleMenu roleMenuParent = new RoleMenu();
            roleMenuParent.setMenuId(menu.getParentId());
            roleMenuParent.setRoleId(roleMenu.getRoleId());
            roleMenuMapper.insert(roleMenuParent);
            // 再添加子菜单权限
            roleMenuMapper.insert(roleMenu);
        }
        return LayuiResult.LayuiData(0,"添加成功",null);
    }


    /**
     * 删除用户菜单权限
     * @param menuId
     * @param roleId
     * @return
     */
    @RequestMapping("/delete/{menuId}/{roleId}")
    public Object delete(@PathVariable Integer menuId,@PathVariable Integer roleId){
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuId).eq("role_id",roleId);
        List<RoleMenu> roleMenusList = roleMenuMapper.selectList(queryWrapper);
        RoleMenu roleMenu = roleMenusList.get(0);
        Menu menu = menuMapper.selectById(roleMenu.getMenuId());
        if(menu.getParentId() == 0){
            QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
            menuQueryWrapper.eq("parent_id",menu.getId());
            List<Menu> menus = menuMapper.selectList(menuQueryWrapper);
            List<Integer> menuIds = new ArrayList<>();
            menus.forEach(menu1 -> {
                menuIds.add(menu1.getId());
            });
            QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
            roleMenuQueryWrapper.in("menu_id",menuIds).eq("role_id",roleId);
            List<RoleMenu> roleMenus = roleMenuMapper.selectList(roleMenuQueryWrapper);
            if(roleMenus.size() > 0){
                return LayuiResult.LayuiData(0,"当前节点下存在子节点权限，暂无法删除",null);
            }
        }
        roleMenuMapper.deleteById(roleMenu.getId());
        return LayuiResult.LayuiData(0,"删除成功",null);
    }


}
